package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.SaUserCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.dto.app.AIReviewReadDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayUserVocSentenceDTO;
import com.cskaoyan.wordmemorize.dto.app.UserFavoriteDTO;
import com.cskaoyan.wordmemorize.dto.app.UserVocBookStatisticsDTO;
import com.cskaoyan.wordmemorize.request.AIReviewReadCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserFavoriteCommand;
import com.cskaoyan.wordmemorize.request.UserVocExampleSentenceCommand;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/priviledge")
public class UserPrivilegeController {

    @Resource
    UserPrivilegeService userPrivilegeService;

    @PostMapping("favorite/cancel")
    public Result cancelFavorite(@RequestBody List<String> ids){
        Long userId = StpKit.USER.getLoginIdAsLong();
        // 将List<String>转换为List<Long>
        List<Long> longIds = ids.stream()
                .map(Long::parseLong)
                .toList();
        userPrivilegeService.cancelFavorite(longIds,userId);
        return Result.ok();
    }

    @PostMapping("favorite/bulk")
    public Result addFavoriteBulk(@RequestBody List<UserFavoriteCommand> userFavoriteCommand){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addFavoriteBulk(userFavoriteCommand,userId);
        return Result.ok();
    }

    @PostMapping("favorite")
    public Result addFavorite(@RequestBody UserFavoriteCommand userFavoriteCommand){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addFavorite(userFavoriteCommand,userId);
        return Result.ok();
    }

    @GetMapping("voc/example/list")
    public Result<PageDTO> getVocExampleList(PageRequest pageRequest){
        long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<DisplayUserVocSentenceDTO> userVocSentence =
                userPrivilegeService.getUserVocSentence(pageRequest, userId);
        return Result.ok(userVocSentence);
    }

    @GetMapping("favorite/list")
    public Result<PageDTO> getUserFavoriteList(PageRequest pageRequest){
        long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<UserFavoriteDTO> userFavoriteList = userPrivilegeService.getUserFavoriteList(pageRequest, userId);
        return Result.ok(userFavoriteList);
    }

    @GetMapping("current/{bookId}/statistics")
    @SaUserCheckPermission("current:book")
    public Result getCurrentBookStatistics(@PathVariable("bookId") String bookId){
        long userId = StpKit.USER.getLoginIdAsLong();
        UserVocBookStatisticsDTO selectedBookStatistics =
                userPrivilegeService.getSelectedBookStatistics(userId, Long.valueOf(bookId));
        return Result.ok(selectedBookStatistics);
    }

    @PostMapping("voc/non")
    public Result vocNotRemember(Long vocId){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.notRemember(vocId, userId);
        return Result.ok();
    }

    @PutMapping("voc/non/cancel")
    public Result vocCancelNotRemember(Long vocId){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.cancelNotRemember(vocId, userId);
        return Result.ok();
    }

    @PostMapping("voc/example")
    public Result addUserVocSentence(@RequestBody UserVocExampleSentenceCommand userVocExampleSentenceCommand){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addUserVocSentence(userVocExampleSentenceCommand, userId);
        return Result.ok();
    }

    @PutMapping("voc/example")
    public Result updateUserVocSentence(@RequestBody UserVocExampleSentenceCommand userVocExampleSentenceCommand){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.updateUserVocSentence(userVocExampleSentenceCommand, userId);
        return Result.ok();
    }

    @PostMapping("review/ai/read/options")
    public Result<List<UserVocAISituationDTO>> getAIReviewReadOptions(){
        List<UserVocAISituationDTO> userVocAISituationDTOS = userPrivilegeService.getAIReviewReadOptions();
        return Result.ok(userVocAISituationDTOS);
    }

    @PostMapping("review/ai/read/free")
    public Result<Integer> getUserVocAIReadRemainFreeCount(){
        Long userId = StpKit.USER.getLoginIdAsLong();
        Integer times = userPrivilegeService.getUserVocAIReadRemainFreeCount(userId);
        return Result.ok(times);
    }

    @PostMapping("review/ai/read")
    public Result<List<AIReviewReadDTO>> getAIReviewReadContent(@RequestBody AIReviewReadCommand aiReviewReadCommand){
        Long userId = StpKit.USER.getLoginIdAsLong();
        List<AIReviewReadDTO> aiReviewReadDTOS = userPrivilegeService.getAIReviewReadContent(aiReviewReadCommand, userId);
        return Result.ok(aiReviewReadDTOS);
    }
}
